package com.zlq.cn.controller.api;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zlq.cn.api.ApiMessage;
import com.zlq.cn.api.ApiParam;
import com.zlq.cn.api.ResponseException;
import com.zlq.cn.cache.RedisCacheService;
import com.zlq.cn.cache.RedisKey;
import com.zlq.cn.config.FinderConfiguration;
import com.zlq.cn.entity.BindUserLocation;
import com.zlq.cn.entity.User;
import com.zlq.cn.service.un.BindUserLocationService;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhoucl
 * @since 2020-12-29
 */
@RestController
@RequestMapping("/api/bindUserLocation")
public class BindUserLocationController {
    
    @Resource
    private BindUserLocationService bindUserLocationService;

    @Resource
    private RedisCacheService redisCacheService;

    @Resource
    private FinderConfiguration finderConfiguration;

    @RequestMapping("/list")
    public Object list(@RequestBody ApiParam apiParam, User loginUser,
                       @NotNull(message = "绑定id不能为空") Integer bindId) {
        return bindUserLocationService.list(new LambdaQueryWrapper<BindUserLocation>()
                .eq(BindUserLocation::getBindUserId, bindId)
                .eq(BindUserLocation::getUserId, loginUser.getId()));
    }

    @RequestMapping("/add")
    public Object add(@RequestBody ApiParam apiParam, User loginUser,
                      @NotNull(message = "绑定id不能为空") Integer bindId,
                      @NotNull(message = "绑定好友用户ID不能为空") Integer toUserId,
                      @NotNull(message = "标签不能为空") String tag,
                      @NotNull(message = "标题不能为空") String title,
                      @NotNull(message = "纬度不能为空") Double latitude,
                      @NotNull(message = "经度不能为空") Double longitude,
                      @NotNull(message = "地址不能为空") String address) {
        int count = bindUserLocationService.count(new LambdaQueryWrapper<BindUserLocation>().eq(BindUserLocation::getBindUserId, bindId));
        if (count < finderConfiguration.getBindUserLocationCount()) {
            BindUserLocation bindUserLocation = new BindUserLocation();
            bindUserLocation.setUserId(loginUser.getId());
            bindUserLocation.setBindUserId(bindId);
            bindUserLocation.setTag(tag);
            bindUserLocation.setToUserId(toUserId);
            bindUserLocation.setTitle(title);
            bindUserLocation.setLatitude(latitude);
            bindUserLocation.setLongitude(longitude);
            bindUserLocation.setAddress(address);
            bindUserLocation.setCreateTime(LocalDateTime.now());
            bindUserLocation.setUpdateTime(LocalDateTime.now());
            bindUserLocationService.save(bindUserLocation);
            List<BindUserLocation> bindUserLocations = redisCacheService.get(RedisKey.createAddressRemind(toUserId));
            if (CollectionUtils.isEmpty(bindUserLocations)) {
                if (bindUserLocations == null) {
                    bindUserLocations = bindUserLocationService.list(new LambdaQueryWrapper<BindUserLocation>().eq(BindUserLocation::getToUserId, toUserId));
                    if (bindUserLocations == null) {
                        bindUserLocations = new ArrayList<>();
                    }
                }
            }
            bindUserLocations.add(bindUserLocation);
            redisCacheService.set(RedisKey.createAddressRemind(toUserId), bindUserLocations);
            return ApiMessage.resultApiMessage("添加成功");
        }
        ResponseException.resultApiError("地址超过上限");
        return null;
    }

    @RequestMapping("/delete")
    public Object delete(@RequestBody ApiParam apiParam, User loginUser,
                         @NotNull(message = "绑定地址id不能为空") Integer bindUserLocationId) {
        BindUserLocation bindUserLocation = bindUserLocationService.getById(bindUserLocationId);
        if (bindUserLocation != null) {
            List<BindUserLocation> bindUserLocations = redisCacheService.get(RedisKey.createAddressRemind(bindUserLocation.getToUserId()));
            if (bindUserLocations != null) {
                bindUserLocations.removeIf(bindUserLocation1 -> bindUserLocation1.getId().equals(bindUserLocationId));
                redisCacheService.set(RedisKey.createAddressRemind(bindUserLocation.getToUserId()), bindUserLocations);
            }
            bindUserLocationService.remove(new LambdaQueryWrapper<BindUserLocation>()
                    .eq(BindUserLocation::getId, bindUserLocationId)
                    .eq(BindUserLocation::getUserId, loginUser.getId()));
        }
        return ApiMessage.resultApiMessage("删除成功");
    }
}

